package com.dtagri.fjwlw.ui;

import java.lang.reflect.Type;
import java.util.List;

import org.apache.http.Header;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import butterknife.InjectView;

import com.dtagri.fjwlw.AppContext;
import com.dtagri.fjwlw.R;
import com.dtagri.fjwlw.adapter.ServerAdapter;
import com.dtagri.fjwlw.api.FJwlwApi;
import com.dtagri.fjwlw.base.BaseActivity;
import com.dtagri.fjwlw.base.TGson;
import com.dtagri.fjwlw.bean.ServerBean;
import com.dtagri.fjwlw.bean.TjUsersEntity;
import com.dtagri.fjwlw.ui.MainActivity.ReloadHandler;
import com.dtagri.fjwlw.util.FJwlwConstant;
import com.dtagri.fjwlw.util.TDevice;
import com.dtagri.fjwlw.util.TToast;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;

/**
 * 服务器适配
 * @author CL.
 * 创建日期:2015-11-19
 */
public class ServerActivity  extends BaseActivity{
	@InjectView(R.id.listview)
	ListView listViewServer;
	List<ServerBean> listData;
	ServerAdapter sAdapter;	
	
	@Override
	protected int getLayoutId() {
		return R.layout.layout_simple_list;
	}

	@Override
	public void initView() {
		setActionBarTitle("服务器适配");
		
		listViewServer.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int arg2,long arg3) {
				TextView tv = (TextView)v.findViewById(R.id.s0);
				String serverIp = tv.getText().toString();
				System.out.println("设置APP服务器地址:"+serverIp);
				AppContext.user.setServerIp(tv.getText().toString());
				TjUsersEntity user = AppContext.user;
				System.out.println("TjUsersEntity:"+user);
				AppContext.saveUserInfo(user);
				for(int i=0;i<listData.size();i++){
					if(serverIp.equals(listData.get(i).getServerIp())){
						listData.get(i).setImageStr(R.drawable.spring_radio2+"");
					}else{
						listData.get(i).setImageStr(R.drawable.spring_radio1+"");
					}
				}
				sAdapter.notifyDataSetChanged();
				Message msg =new Message();
				msg.arg1 = FJwlwConstant.Handler_flag_ad;
				msg.obj = serverIp;
				MainActivity.handler.sendMessage(msg);
			}
		});
	}

	@Override
	public void initData() {
		if (!prepareForLogin()) {
			return;
		}
		FJwlwApi.getServerList(mHandlerServer);
	}
	
	@Override
	public void onClick(View arg0) {
		final int id = arg0.getId();
		Intent intent;
		switch (id) {
		case R.id.listview:
			break;
		}
	}	
	
	private final AsyncHttpResponseHandler mHandlerServer = new AsyncHttpResponseHandler() {
		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,Throwable arg3) {
		}

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
			Type type = new TypeToken<List<ServerBean>>() {
				}.getType();
				listData = TGson.gson.fromJson(new String(arg2), type);
			String serverIp = AppContext.user.getServerIp();
			for(int i=0;i<listData.size();i++){
				listData.get(i).setImageStr(R.drawable.spring_radio1+"");
				if(serverIp!=null){
					if(serverIp.equals(listData.get(i).getServerIp())){
						listData.get(i).setImageStr(R.drawable.spring_radio2+"");
					}
				}
			}
			System.out.println("serverActivity-serverIp:"+AppContext.user.getServerIp());
			sAdapter = new ServerAdapter(ServerActivity.this, listData);
			listViewServer.setAdapter(sAdapter);
			
			
	
		}
	};
	private boolean prepareForLogin() {
		if (!TDevice.hasInternet()) {
			TToast.showToastShort("当前没有可用的网络链接");
			return false;
		}
		return true;
	}
}
